package com.autologistics.action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.autologistics.model.Admin;
import com.autologistics.model.Machine;
import com.autologistics.model.Machineaddress;
import com.autologistics.service.AdminService;
import com.autologistics.service.MachineService;
import com.autologistics.service.MachineaddressService;
import com.autologistics.util.GobalValue;
import com.autologistics.util.PageBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class MachineAction extends ActionSupport implements ModelDriven<Machine>{
	private Machine machine =new Machine();
	private MachineService machineService;
	private MachineaddressService machineaddressService;
	private AdminService adminService;
	private List<Admin> listadmin;
	private List<Machine> listmachine;
	private List<Machineaddress> listmachineaddress;
	private int address1;
	private String address2;
	private int renId;
	private int renrole;
	private String renname;
	private String message;

	private PageBean pageBean;
	//默认是1
	private int page=1;
	
	

	/**
	 * 列出所有机器
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String listAllMachine(){
		if (!checkLogin()){
			return "loginfail";
		}
		if (renrole!=0 && renrole!=1){
			return "wrongrole";
		}
		listmachine= machineService.listAll();
		if (listmachine==null){
			message="暂无自助终端";
			return SUCCESS;
		}
		pageBean = machineService.queryForOnePage(GobalValue.pageSize, page, listmachine.size());
		listmachine=pageBean.getMachineList();
		for (Machine m:listmachine){

			Machineaddress ma = machineaddressService.getById(m.getMachineaddress().getId());
			ma.setMachineaddress(machineaddressService.getById(ma.getMachineaddress().getId()));
			m.setMachineaddress(ma);
			Admin a = adminService.getById(m.getAdmin().getId());
			m.setAdmin(a);
		}
		return SUCCESS;
	}
	
	/**
	 * 准备添加所有机器
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String preAddMachine(){
		if (!checkLogin()){
			return "loginfail";
		}
		if (renrole!=0 && renrole!=1){
			return "wrongrole";
		}
		listmachineaddress=machineaddressService.getByParent(0);
		if (listmachineaddress==null){
			message="没有一级地址请添加";
			return "error";
		}
		listadmin=adminService.getByRole(1);
		return SUCCESS;
	}
	/**
	 * 添加机器
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String addMachine(){
		if (!checkLogin()){
			return "loginfail";
		}
		if (renrole!=0 && renrole!=1){
			return "wrongrole";
		}
		Timestamp ts = new Timestamp(new Date().getTime());
		machine.setJoinTime(ts);
		machine.setState(1);
		Machineaddress ma = new Machineaddress();
		ma.setName(address2);
		ma.setMachineaddress(machineaddressService.getById(address1));
		machine.setMachineaddress(machineaddressService.getById(machineaddressService.save(ma)));
		machine.setNumRepair(0);
		machine.setNumUsed(0);
		machineService.add(machine);
		return this.listAllMachine();
	}
	/**
	 * 准备修改机器
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String preEditMachine(){
		if (!checkLogin()){
			return "loginfail";
		}
		if (renrole!=0 && renrole!=1){
			return "wrongrole";
		}
		machine=machineService.getById(machine.getId());
		listmachineaddress=machineaddressService.getByParent(0);
		listadmin=adminService.getByRole(1);
		machine.setAdmin(adminService.getById(machine.getAdmin().getId()));
		Machineaddress ma = machineaddressService.getById(machine.getMachineaddress().getId());
		ma.setMachineaddress(machineaddressService.getById(ma.getMachineaddress().getId()));
		machine.setMachineaddress(ma);
		return SUCCESS;
	}
	/**
	 * 修改机器
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String editMachine(){
		if (!checkLogin()){
			return "loginfail";
		}
		if (renrole!=0 && renrole!=1){
			return "wrongrole";
		}
		machineService.updata(machine);
		machineaddressService.update(machine.getMachineaddress());
		return this.listAllMachine();
	}
	public boolean checkLogin(){
		Map<String, Object> attibutes = ActionContext.getContext().getSession();
		if (attibutes.get("id")==null){
			message="未登陆，请重新登陆";
			return false;
		}
		String s=attibutes.get("id").toString();
		renId=Integer.parseInt(s);
		if (attibutes.get("role")!=null){
			s=attibutes.get("role").toString();
			renrole=Integer.parseInt(s);
		}
		renname=attibutes.get("name").toString();
		return true;
	}
	
	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRenId() {
		return renId;
	}

	public void setRenId(int renId) {
		this.renId = renId;
	}

	public int getRenrole() {
		return renrole;
	}

	public void setRenrole(int renrole) {
		this.renrole = renrole;
	}

	public String getRenname() {
		return renname;
	}

	public void setRenname(String renname) {
		this.renname = renname;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public int getAddress1() {
		return address1;
	}

	public void setAddress1(int address1) {
		this.address1 = address1;
	}

	public String getAddress2() {
		return address2;
	}

	public void setAddress2(String address2) {
		this.address2 = address2;
	}

	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	public List<Admin> getListadmin() {
		return listadmin;
	}

	public void setListadmin(List<Admin> listadmin) {
		this.listadmin = listadmin;
	}

	public List<Machineaddress> getListmachineaddress() {
		return listmachineaddress;
	}

	public void setListmachineaddress(List<Machineaddress> listmachineaddress) {
		this.listmachineaddress = listmachineaddress;
	}

	public MachineaddressService getMachineaddressService() {
		return machineaddressService;
	}

	public void setMachineaddressService(MachineaddressService machineaddressService) {
		this.machineaddressService = machineaddressService;
	}

	public Machine getMachine() {
		return machine;
	}

	public void setMachine(Machine machine) {
		this.machine = machine;
	}

	public MachineService getMachineService() {
		return machineService;
	}

	public void setMachineService(MachineService machineService) {
		this.machineService = machineService;
	}

	public List<Machine> getListmachine() {
		return listmachine;
	}

	public void setListmachine(List<Machine> listmachine) {
		this.listmachine = listmachine;
	}
	
	@Override
	public Machine getModel() {
		// TODO Auto-generated method stub
		return this.machine;
	}

}
